# -*- coding: UTF-8 -*-
import os
import sys

from os.path import dirname

root = dirname(dirname(__file__))
sys.path.append(root)
os.chdir(root)

'''
Created on 03.02.2013

@author: Peter He�, Germany

samples from vvvv how they do it:

Centered/-/-/Phase=.50/Count=11
0.0455, 0.1364, 0.2273, 0.3182, 0.4091, -0.5000, -0.4091, -0.3182, -0.2273, -0.1364, -0.0455

Centered/-/-/Phase=.75/Count=11
0.2955, 0.3864, 0.4773, -0.4318, -0.3409, -0.2500, -0.1591, -0.0682, 0.0227, 0.1136, 0.2045

'''

class BasePi3DObjectWrapper:
    
    def __init__(self, 
                 _pi3dShape,                     # a Pi3D object derrived from shape
                 texture=None,              # list of Pi3D texture objects
                 shader=None,               # list of Pi3D shader objects
                 shaderCustomData=None      # list of floats 
                 ):
        self._pi3dShape = _pi3dShape
        self._texture = texture
        self._shader = shader
        self._shaderCustomData =  shaderCustomData


    def prepareDraw(self, 
              ordinalNr, 
              transform, 
              texture=None,
              shaderCustomData=None
              ):    
        self._ordinalNr = ordinalNr
        self._transform = transform
        if (texture!=None): self._texture = texture
        if (shaderCustomData!=None): self._shaderCustomData=shaderCustomData


    def draw(self):
        
        # print "do draw\n\r"
        
        # do the first transformation
        tx = self._transform.translateX[self._ordinalNr]
        ty = self._transform.translateY[self._ordinalNr]
        sx = self._transform.scaleX[self._ordinalNr]
        sy = self._transform.scaleY[self._ordinalNr]
        rot = self._transform.rotate[self._ordinalNr] * 360
        # print "ordinalnr, tx, ty, sx, sy, rot:", self._ordinalNr, " ", tx, " ", ty, sx, sy, rot, "\r"

        # zeichne nun das Pi3d Object
        self._pi3dShape.scale(sx,sy,0)
        self._pi3dShape.position(tx, ty, 0)
        self._pi3dShape.rotateToZ(rot)

        if (self._shader != None and self._shaderCustomData != None):
            # Sample for Star shader:
            # uniform float time =========> unif[16][0] tm
            # uniform float fScale =======> unif[16][1] sc
            # uniform float fOriginShift => unif[16][2] -0.5 * sc
            self._pi3dShape.set_custom_data(48, self._shaderCustomData) # _shaderCustomData must be len of 3, 6, ...
        
        # print "shader:", self._shader, " tex:", self._texture
        self._pi3dShape.draw(shader=self._shader, txtrs=[self._texture]);
        